//
//  MainEntryController.m
//  healthApp
//
//  Created by william mu on 12-8-28.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "MainEntryController.h"
#import "HealthInfoController.h"
#import "RequestManageController.h"
#import "CareAboutOthersController.h"
#import "EagerForCareController.h"
#import "BottomBarController.h"
#import "ProtocolTranslate.h"
#import "RootController.h"
#import "WaitUtility.h"

@interface MainEntryController ()

@end

@implementation MainEntryController
@synthesize symptomDesc;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"主页";
    
    RootController* root = [RootController sharedInstance];
}

-(void) onBottomBarChanged:(NSString*) symptom
{
    NSRunLoop* loop = [NSRunLoop currentRunLoop];
    
    [loop cancelPerformSelector:@selector(startRequest) target:self argument:nil];
    
    [self performSelector:@selector(startRequest) withObject:nil afterDelay:0.5];
}

-(void) viewWillAppear:(BOOL)animated
{
    [m_allRequest release], m_allRequest = [[NSMutableArray alloc] init];
    [self startRequest];
}

-(void) viewWillDisappear:(BOOL)animated
{
    ProtocolService* protocol = [ProtocolService sharedInstance];
    [protocol shutdownAnyResponse:m_allRequest];
}

-(void) startRequest
{
    RootController* root = [RootController sharedInstance];

    ProtocolService* protocol = [ProtocolService sharedInstance];
    GlobalState* state = [GlobalState sharedInstance];
    NSString* uid = state.uid;
    [protocol setTarget:self];
    [protocol setAction:@selector(onGetSymptom:)];
    NSString* seq = [protocol getAllSymptom:uid];
    [m_allRequest addObject:seq];
}

-(void) onGetSymptom:(NSString*) json
{
    NSArray* symptoms = translate_symptom(json);
    
    NSString* symptomDesc = get_symptoms_desc(symptoms, 15);
    
    self.symptomDesc.text = symptomDesc;
}

-(void) onBottomBarTap:(NSNotification*) notification
{
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction)onBtnPress:(id)sender
{
    UIButton* btn = (UIButton*) sender;
    
    if(btn.tag == 0)
    {
        HealthInfoController* controller = [[HealthInfoController alloc] init];
        [self.navigationController pushViewController:controller animated:YES];
        [controller release];
    }
    else if(btn.tag == 1)
    {
        RequestManageController* controller = [[RequestManageController alloc] init];
        [self.navigationController pushViewController:controller animated:YES];
        [controller release];
    }
    else if(btn.tag == 2)
    {
        CareAboutOthersController* controller = [[CareAboutOthersController alloc] init];
        [self.navigationController pushViewController:controller animated:YES];
        [controller release];
    }
    else if(btn.tag == 3)
    {
        EagerForCareController* controller = [[EagerForCareController alloc] init];
        [self.navigationController pushViewController:controller animated:YES];
        [controller release];
    }
    else {
        NSAssert(false, @"Impossible!");
    }

}


@end
